<?php
namespace app\modules\api\controllers;

use yii;
use app\models\Address;
use ac\component\tool\Tool;
use ac\component\exception\ErrMap;
use app\models\OperationLog;



class AddressController extends AuthController
{

      /*收货地址列表*/
    public function actionAddressList()
    {
        $mid = $this->userInfo->member_id;
        $get = Yii::$app->request->get();
        if(isset($get['page'])){
            $currPage = (int)$get['page']?$get['page']:1;
        }else{
            $currPage = 1;
        }
        $address = new Address;

        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $address = new Address;
        $count = $address::find()->count();
        $addressList = $address::find()->where('member_id = :mid', [':mid' => $mid])
                        ->orderBy(['address_id' => SORT_DESC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        $res = array(
            'list' => $addressList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        // P($res);
        return Tool::showRes([0, '获取成功'], $res);

    }

    /*添加*/
    public function actionAddAddress()
    {
        if(isset($this->userInfo->member_id)) {
            $mid = $this->userInfo->member_id;
        }
        if(Yii::$app->request->isPost){
        // if(1){
            // $this->post = array(
            //     'Address' => array(
            //         'member_id' => $mid,
            //         'consignee' => '见哥',
            //         'sex' => 1,
            //         'phone' => '13915028703',
            //         'province' => '120000',
            //         'city' => '120100',
            //         'district' => '120104',
            //         'address' => '怡美花园',
            //         'zipcode' => '213000',
            //         'sign_building' => '路径',
            //         'is_default' => 2,
            //     )
            // );
            $this->post["member_id"] = $mid;
            $data = [
                "Address" => $this->post
            ];
            // P($data);
            $address = new Address;
            if($address->addAddress($data, $mid)){
                $backInfo = [0, '添加地址成功'];//返回信息
            }else{
                if($address->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }


    /*修改地址*/
    public function actionModifyAddress()
    {
        if(isset($this->userInfo->member_id)) {
            $mid = $this->userInfo->member_id;
        }
        // $mid = 1;
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            // P($this->get);
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $address = Address::find()
                            ->where('address_id = :id and member_id = :mid', [':id' => $id, ':mid' => $mid])
                            ->asArray()->one();
                // P($address);
                if(!is_null($address)){
                    // P($address);
                    return Tool::showRes([0, '获取成功'], $address);
                };
            }
        }


        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
        // if(1){
            // $this->post = array(
            //     'id' => 3,
            //     'consignee' => '见哥333',
            //     'sex' => '1',
            //     'phone' => '13915028733',
            //     'province' => '120333',
            //     'city' => '120133',
            //     'district' => '120133',
            //     'address' => '怡美花园33',
            //     'zipcode' => '2130333',
            //     'sign_building' => '路径333',
            //     'is_default' => 2,
            // );
            $this->post["member_id"] = $mid;
            $id = isset($this->post['id'])?intval($this->post['id']):'';
            $data = [
                "Address" => $this->post
            ];
            // P($data);
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $address = new Address;
                if($address->modifyAddress($data, $id, $mid)){
                    $backInfo = [0, '修改收货地址成功'];//返回信息
                }else{
                    if($address->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $address->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除*/
    public function actionDeleteAddress()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $address = new Address;
            if($address->deleteAll('address_id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除地址成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除地址失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
